package com.luo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.luo.constant.MessageConstant;
import com.luo.entity.PageResult;
import com.luo.entity.QueryPageBean;
import com.luo.entity.Result;
import com.luo.pojo.TravelGroup;
import com.luo.service.TravelGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author Zoro
 * @date 2021年08月25
 */
@RestController
@RequestMapping("/travelGroup")
public class TravelGroupController {

    @Reference
    private TravelGroupService travelGroupService;


    /**
     * 分页查询
     */
    @PreAuthorize("hasAuthority('TRAVELGROUP_QUERY')")
    @RequestMapping("/pageSelect.do")
    public PageResult pageSelect(@RequestBody QueryPageBean queryPageBean) {
        PageInfo<TravelGroup> travelGroupPageInfo = travelGroupService.pageSelect(queryPageBean);

        return new PageResult(travelGroupPageInfo.getTotal(), travelGroupPageInfo.getList());
    }

    /**
     * 添加数据
     */
    @PreAuthorize("hasAuthority('TRAVELGROUP_ADD')")
    @RequestMapping("/add.do")
    public Result travelGroupAdd(Integer[] travelItemIds, @RequestBody TravelGroup travelGroup) {

        try {
            travelGroupService.travelGroupAdd(travelItemIds, travelGroup);
            return new Result(true, MessageConstant.ADD_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_TRAVELGROUP_FAIL);
        }
    }

    /**
     * 根据id获取数据
     */
    @PreAuthorize("hasAuthority('TRAVELGROUP_QUERY')")
    @RequestMapping("/getTravelGroup.do")
    public Result getTravelGroupById(Integer travelGroupId) {
        try {
            TravelGroup travelGroup = travelGroupService.getTravelGroupById(travelGroupId);
            return new Result(true, MessageConstant.QUERY_TRAVELGROUP_SUCCESS, travelGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

    /**
     * 修改数据
     */
    @PreAuthorize("hasAuthority('TRAVELGROUP_EDIT')")
    @RequestMapping("/travelGroupEdit.do")
    public Result travelGroupEdit(Integer[] travelItemIds, @RequestBody TravelGroup travelGroup) {

        try {
            travelGroupService.travelGroupEdit(travelItemIds, travelGroup);
            return new Result(true, MessageConstant.EDIT_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_TRAVELGROUP_FAIL);
        }
    }

    /**
     * 删除数据
     */
    @PreAuthorize("hasAuthority('TRAVELGROUP_DELETE')")
    @RequestMapping("/delete.do")
    public Result travelGroupDeleteById(Integer travelGroupId) {

        try {
            travelGroupService.travelGroupDeleteById(travelGroupId);
            return new Result(true, MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.DELETE_TRAVELGROUP_FAIL);
        }

    }

    /**
     * 查询全部数据
     */
    @PreAuthorize("hasAuthority('TRAVELGROUP_QUERY')")
    @RequestMapping("/selectAll")
    public List<TravelGroup> selectAll() {
        return travelGroupService.selectAll();

    }
}
